package com.fan.leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * 451. 根据字符出现频率排序
 *
 * @author ZhangFan
 * @since 2021/8/3 下午 10:23
 */
public class Question451 {
    public String frequencySort(String s) {
        Map<Character, String> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            map.put(s.charAt(i), map.getOrDefault(s.charAt(i), "") + s.charAt(i));
        }
        StringBuilder str = new StringBuilder();
        map.entrySet().stream()
                .sorted((o1, o2) -> o2.getValue().length() - o1.getValue().length())
                .forEach(e -> str.append(e.getValue()));

        return str.toString();
    }
}
